package com.definesys.smecrailtransit.adapter.SelectUserAdapter;

import android.databinding.DataBindingUtil;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.definesys.smecrailtransit.R;
import com.definesys.smecrailtransit.api.SmecRailApi;
import com.definesys.smecrailtransit.base.SmecBaseRecyclerViewHolder;


import com.definesys.smecrailtransit.bean.ApplyBean;
import com.definesys.smecrailtransit.databinding.LayoutSelectUserItemBinding;
import com.definesys.smecrailtransit.databinding.LayoutUnselectUserItemBinding;
import com.definesys.smecrailtransit.module.MonthForm.MonthFormFragment;
import com.definesys.smecrailtransit.module.RealStatus.RealStatusFragment;
import com.definesys.smecrailtransit.module.TotalTime.TotalTimeFragment;
import com.hwangjr.rxbus.Bus;

import java.util.ArrayList;

/**
 * Created by cjy on 2017/7/27.
 */

public class SelectUserAdapter extends RecyclerView.Adapter<SmecBaseRecyclerViewHolder> {
    private int SELECT = 1;
    private int UNSELECT = 0;
    private ArrayList<ApplyBean> list;
    public String select_user;
    protected Bus mRxbus;
    private String type;

    public SelectUserAdapter(Bus mRxbus,String type){

        this.list = new ArrayList<>();
        this.select_user = "全部运营商";

        this.mRxbus = mRxbus;
        this.type = type;
    }

    public void setList(ArrayList<ApplyBean> list){
        this.list = list;
    }

    @Override
    public SmecBaseRecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        if(viewType == SELECT){
            LayoutSelectUserItemBinding layoutUserBinding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.layout_select_user_item,parent,false);
            SmecBaseRecyclerViewHolder smecBaseRecyclerViewHolder=new
                    SmecBaseRecyclerViewHolder(layoutUserBinding.getRoot());
            smecBaseRecyclerViewHolder.setViewType(viewType);
            smecBaseRecyclerViewHolder.setBinding(layoutUserBinding);
            return smecBaseRecyclerViewHolder;
        }else{
            LayoutUnselectUserItemBinding layoutUserBinding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.layout_unselect_user_item,parent,false);
            SmecBaseRecyclerViewHolder smecBaseRecyclerViewHolder=new
                    SmecBaseRecyclerViewHolder(layoutUserBinding.getRoot());
            smecBaseRecyclerViewHolder.setViewType(viewType);
            smecBaseRecyclerViewHolder.setBinding(layoutUserBinding);
            return smecBaseRecyclerViewHolder;
        }
    }

    @Override
    public void onBindViewHolder(SmecBaseRecyclerViewHolder holder, int position) {
        if(holder.getViewType() == SELECT){
            LayoutSelectUserItemBinding viewDataBinding=(LayoutSelectUserItemBinding)holder.getBinding();
            viewDataBinding.text.setText(list.get(position).getName());
            if(position == list.size() -1 ){
                viewDataBinding.line.setVisibility(View.GONE);
            }else{
                viewDataBinding.line.setVisibility(View.VISIBLE);
            }
            viewDataBinding.item.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(SelectUserAdapter.this.type.equals(SmecRailApi.MonthCode.MONTHFORM)){
                        mRxbus.post(MonthFormFragment.CHANGE_USER,SelectUserAdapter.this.list.get(position).getName());
                    }
                    if(SelectUserAdapter.this.type.equals(SmecRailApi.MonthCode.TOTALTIME)){
                        mRxbus.post(TotalTimeFragment.CHANGE_USER,SelectUserAdapter.this.list.get(position).getName());
                    }
                    if(SelectUserAdapter.this.type.equals(SmecRailApi.MonthCode.REALSTATUS)){
                        mRxbus.post(RealStatusFragment.CHANGE_USER,SelectUserAdapter.this.list.get(position).getName());
                    }
                }
            });

        }else{
            LayoutUnselectUserItemBinding viewDataBinding=(LayoutUnselectUserItemBinding)holder.getBinding();
            viewDataBinding.text.setText(list.get(position).getName());
            if(position == list.size() -1 ){
                viewDataBinding.line.setVisibility(View.GONE);
            }else{
                viewDataBinding.line.setVisibility(View.VISIBLE);
            }
            viewDataBinding.item.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(SelectUserAdapter.this.type.equals(SmecRailApi.MonthCode.MONTHFORM)){
                        mRxbus.post(MonthFormFragment.CHANGE_USER,SelectUserAdapter.this.list.get(position).getName());
                    }
                    if(SelectUserAdapter.this.type.equals(SmecRailApi.MonthCode.TOTALTIME)){
                        mRxbus.post(TotalTimeFragment.CHANGE_USER,SelectUserAdapter.this.list.get(position).getName());
                    }
                    if(SelectUserAdapter.this.type.equals(SmecRailApi.MonthCode.REALSTATUS)){
                        mRxbus.post(RealStatusFragment.CHANGE_USER,SelectUserAdapter.this.list.get(position).getName());
                    }
                }
            });
        }

    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    @Override
    public int getItemViewType(int position) {
        if(list.get(position).getName().equals(this.select_user)){
            return SELECT;
        }else {
            return UNSELECT;
        }
    }
}
